package com.fy56.platform.goodsTracking.quartz;

import com.fy56.platform.goodsTracking.api.GoodsTrackApi;
import com.fy56.platform.goodsTracking.commons.DateUtil;
import com.fy56.platform.goodsTracking.jdbc.datasource.DBManage;
import com.fy56.platform.goodsTracking.model.SysTenant;
import com.fy56.platform.goodsTracking.service.TenantService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CarCollect {
    @Autowired
    private TenantService tenantService;
    @Autowired
    private GoodsTrackApi goodsTrackService;
    @Autowired
    private DBManage dbManage;

    static Logger log = Logger.getLogger(CarCollect.class);

    public void addJob() {
        //查询所有租户
        List<SysTenant> list = tenantService.QueryAllTenant();
        int iloop = 0;
        log.info("执行收集操作(" + DateUtil.Now() + ")");
        while (iloop < list.size()) {
            log.info("Datasource:" + list.get(iloop).getDatasource() +
                    ";Databasename:" + list.get(iloop).getDatabasename()
                    + ";---开始---");
            if(list.get(iloop).getDatabasename()!=null&&list.get(iloop).getDatabasename().length()!=0){
                try {
                    dbManage.setDBLink(list.get(iloop).getDatasource(), list.get(iloop).getDatabasename());
                        goodsTrackService.insert_car(list.get(iloop).getDatasource()
                                , list.get(iloop).getDatabasename(),list.get(iloop).getWxpublicnumber(),list.get(iloop).getAppid());

                } catch (Exception e) {
                    log.error(";Datasource:" + list.get(iloop).getDatasource()
                            + ";Databasename:" + list.get(iloop).getDatabasename()
                            + ";---错误---" + e.getMessage());
                }
            }
            iloop++;
        }

        try {
            Thread.sleep(10000);//休息一会
        } catch (InterruptedException ire) {
        }
    }
    public void addJobB(String begin,String end) {
        //查询所有租户
        List<SysTenant> list = tenantService.QueryAllTenant();
        int iloop = 0;
        log.info("执行收集操作(" + DateUtil.Now() + ")");
        while (iloop < list.size()) {
            log.info("Datasource:" + list.get(iloop).getDatasource() +
                    ";Databasename:" + list.get(iloop).getDatabasename()
                    + ";---开始---");
            if(list.get(iloop).getDatabasename()!=null&&list.get(iloop).getDatabasename().length()!=0){
                try {
                    dbManage.setDBLink(list.get(iloop).getDatasource(), list.get(iloop).getDatabasename());
                        goodsTrackService.insert_car_buchuan(list.get(iloop).getDatasource()
                                , list.get(iloop).getDatabasename(),list.get(iloop).getWxpublicnumber(),list.get(iloop).getAppid(),begin,end,"");

                } catch (Exception e) {
                    log.error(";Datasource:" + list.get(iloop).getDatasource()
                            + ";Databasename:" + list.get(iloop).getDatabasename()
                            + ";---错误---" + e.getMessage());
                }
            }
            iloop++;
        }

    }
}
